/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pima.gestrest.controller;

import com.pima.gestrest.business.CuisineEJB;
import com.pima.gestrest.model.Cuisine;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.ejb.EJB;
import java.util.Iterator;
import java.util.List;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

/**
 *
 * @author Pierre-Louis Conte
 */

@ManagedBean (name = "cuisineController")
@SessionScoped
public class CuisineController {

     // ======================================
    // =             Attributes             =
    // ======================================

    @EJB
    private CuisineEJB cuisineEJB;

    private HtmlDataTable dataTable;
    private Cuisine cuisine = new Cuisine();
    private ListDataModel cuisineList;
    private ArrayList<SelectItem> itemList;

    private void updateItemList () {
        itemList = new ArrayList<SelectItem>(cuisineEJB.getItems());
    }

    private void updateCuisineList() {
        cuisineList = new ListDataModel(cuisineEJB.findAll());
    }

     // ======================================
    // =           Public Methods           =
    // ======================================

    public String doNew() {
        cuisine = new Cuisine();
        return "../secured/newCuisine.faces";
    }

    public String doCreate() {
        cuisineEJB.create(cuisine);
        return "../resources/listCuisines.faces";
    }

    public String doDelete() {
        List<Cuisine> cuisines = (List<Cuisine>)cuisineList.getWrappedData();
        cuisineEJB.delete(onlySelected(cuisines));
        return "../resources/listCuisines.faces";
    }

    private List<Cuisine> onlySelected(List<Cuisine> list) {
        for (Iterator<Cuisine> it = list.iterator(); it.hasNext(); ) {
            if (!(it.next().isSelected()))
                it.remove();
        }
        return list;
    }

    public String viewDetail () {
        cuisine = (Cuisine)cuisineList.getRowData();
        return "../resources/viewCuisine.faces";
    }

    public String doEdit() {
        //cuisine = (Cuisine)cuisineList.getRowData(); // Voici comment on trouve le livre sélectionné
        return "../secured/editCuisine.faces";
    }

    public String doSave() {
        cuisineEJB.update(cuisine);
        return "../resources/listCuisines.faces";
    }

    // ======================================
    // =          Getters & Setters         =
    // ======================================

    public Cuisine getCuisine() {
        return cuisine;
    }

    public void setCuisine(Cuisine cuisine) {
        this.cuisine = cuisine;
    }

    public ListDataModel getCuisineList() {
        updateCuisineList();
        return cuisineList;
    }

    public void setCuisineList(ListDataModel cuisineList) {
        this.cuisineList = cuisineList;
    }

    public HtmlDataTable getDataTable() {
        return dataTable;
    }

    public void setDataTable(HtmlDataTable dataTable) {
        this.dataTable = dataTable;
    }

    public List<SelectItem> getItemList ()
    {
        updateItemList();
        return itemList;
    }

    public void setItemList (ArrayList<SelectItem> list)
    {
        this.itemList = list;
    }
}
